import { get } from '@/http/axios.js'
const actions = {
  // 获取所有订单信息
  getMyOrder(context,list) {
    let data = {
      ...list,
      customerId:localStorage.getItem('userId'),
    }
    get('/order/pageQuery', data).then(res => {
      res = res.data.data.list
        context.commit('SET_ORDERDATA', res)
    })
  },
  // 获取地址信息
  getAllAddress(context,list){
    let data = {
      ...list,
      userId:localStorage.getItem('userId'),
    }
    get('/address/pageQuery',data).then(res=>{
      res=res.data.data.list
      for(let item of res){
          item.address=item.province+item.city+item.area+item.address
          item.tel=item.telephone
          item.name=item.username
      }
      res[0].isDefault = 1
      let defaultAddress =res[0]
      context.commit('SET_ADDRESS', res)
      context.commit('SET_DEFAULTADDRESS', defaultAddress)
    })
    
  }
}
const mutations = {
  SET_ORDERDATA(state, response) {
    state.orderData = response
  },
  SET_ADDRESS(state, response) {
    state.addressInfo = response
  },
  SET_DEFAULTADDRESS(state, response) {
    state.defaultAddress = response
  },
}
const state = {
  orderData: [],
  addressInfo:[],
  defaultAddress:{},
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
}